package com.ylq.impl;

import com.ylq.dao.ModulesDao;
import com.ylq.entity.Modules;
import com.ylq.entity.Roles;
import com.ylq.entity.TreeDto;
import com.ylq.service.ModulesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author qiang
 * @Description TODO
 * @Date 2020-04-30
 **/
@Service
public class ModulesServiceImpl implements ModulesService{
    @Autowired
    private ModulesDao modulesDao;


    @Override
    public int createModule(Modules modules) {
        return modulesDao.createModule(modules);
    }

    @Override
    public int updateModule(Modules modules) {
        return modulesDao.updateModule(modules);
    }

    @Override
    public int deleteModule(int mId) {
        return modulesDao.deleteModule(mId);
    }

    @Override
    public int deleteMo(int mId) {
        return modulesDao.deleteMo(mId);
    }








    public List<Modules> queryAll() {
        // TODO Auto-generated method stub
        return modulesDao.queryAll();
    }
    //将查询的模块信息封装成树节点
    public List<TreeDto> changeTreeNode(){


        //获取模块信息
        List<Modules> list = queryAll();


        //声明树节点集合
        List<TreeDto> treeList = new ArrayList<TreeDto>();
        for(Modules module:list) {
            TreeDto tree = new TreeDto();
            tree.setId(module.getId());
            tree.setTitle(module.getTitle());
            tree.setPid(module.getParentId());
            tree.setChecked(false);
            tree.setHref(module.getPath());
            treeList.add(tree);
        }
        return treeList;
    }
    public List<TreeDto> loadTree() {
        List<TreeDto> list = changeTreeNode();
        //声明一个集合存根节点
        List<TreeDto> rootList = new ArrayList<TreeDto>();
        for(TreeDto tree:list) {
            if(tree.getPid()==0) {
                rootList.add(getchildren(list,tree));
            }
        }
        return rootList;
    }

    //获取子节点的方法
    public TreeDto getchildren(List<TreeDto> rootList,TreeDto dto) {
        //声明一个集合存储子类节点
        List<TreeDto> childrenList = new ArrayList<TreeDto>();
        for(TreeDto tree:rootList) {
            if(tree.getPid()==dto.getId()) {
                if(dto.getChildren()==null) {
                    dto.setChildren(childrenList);
                }
                dto.getChildren().add(getchildren(rootList,tree));
            }
        }
        return dto;
    }
    public List<Modules> selByRoleName(List<String> roleNames) {
        // TODO Auto-generated method stub
        return modulesDao.selByRoleName(roleNames);
    }



/*    @Override
    public List<Modules> getSysRightLayui(String[] roleNames) {
        List<Modules> objects = new ArrayList<Modules>();
        for (String rms: roleNames){
            objects =modulesDao.getSysRightLayui(rms);
        }
        return objects;
    }*/

  /*  //将查询的模块信息封装成树节点
    public List<TreeDto> changeTreeNode(String[] roleName){
        //获取模块信息
        List<Modules> list = selByRoleName(roleName);
        System.out.println("list=>"+list);
        //声明树节点集合
        List<TreeDto> treeList = new ArrayList<TreeDto>();
        for(Modules module:list) {
            TreeDto tree = new TreeDto();
            tree.setId(module.getId());
            tree.setTitle(module.getTitle());
            tree.setPid(module.getParentId());
            tree.setChecked(false);
            tree.setHref(module.getPath());
            treeList.add(tree);
        }
        return treeList;
    }
    public List<TreeDto> getsysRight(String[] roleNames) {
        List<TreeDto> list = changeTreeNode(roleNames);
        System.out.println("转换之后list=>"+list);
        //声明一个集合存根节点
        List<TreeDto> rootList = new ArrayList<TreeDto>();
        for(TreeDto tree:list) {
            if(tree.getPid()==0) {
                rootList.add(getchildren(list,tree));
            }
        }
        return rootList;
    }*/
//将查询的模块信息封装成树节点
  public List<TreeDto> changeTreeNode(List<String> roleName){
      //获取模块信息
      List<Modules> list = selByRoleName(roleName);
      System.out.println("list=>"+list);
      //声明树节点集合
      List<TreeDto> treeList = new ArrayList<TreeDto>();
      for(Modules module:list) {
          TreeDto tree = new TreeDto();
          tree.setId(module.getId());
          tree.setTitle(module.getTitle());
          tree.setPid(module.getParentId());
          tree.setChecked(false);
          tree.setHref(module.getPath());
          treeList.add(tree);
      }
      return treeList;
  }
    public List<TreeDto> getsysRight(List<String> roleNames) {
        List<TreeDto> list = changeTreeNode(roleNames);
        System.out.println("转换之后list=>"+list);
        //声明一个集合存根节点
        List<TreeDto> rootList = new ArrayList<TreeDto>();
        for(TreeDto tree:list) {
            if(tree.getPid()==0) {
                rootList.add(getchildren(list,tree));
            }
        }
        return rootList;
    }


}
